// pages/mine/perfectinfo/perfectinfo.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      user: wx.getStorageSync('user')
    })
    this.setData({
      fileList:this.data.user.icon==null?[]:[{url:"https://fileapi.wechat.copyctrl.top/data/upload/picture/"+this.data.user.icon}]
    })
  },
  //上传图片之前
  beforeRead(event) {
    const { file, callback } = event.detail;
    console.log(file)
    if (file.size > 10 * 1024 * 1024) {
      wx.showToast({
        title: '文件大小不能超过 10M!',
        icon: 'none'
      })
      callback(false);
    }
    let name = file.url.substring(file.url.lastIndexOf("."));
    if (name != '.png' && name != '.jpg') {
      wx.showToast({
        title: '图片必须是 .png 或 .jpg!',
        icon: 'none'
      })
      callback(false);
    }
    callback(true)
  },
  //上传图片
  afterRead(event) {
    console.log('上传图片')
    const {
      file
    } = event.detail;
    let that = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.globalData.fileApi+'/pic',
      method: 'POST',
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      header: {
        token: wx.getStorageSync('token')
      },
      success(res) {
        var data = JSON.parse(res.data)
        const {
          fileList = []
        } = that.data.fileList;
        fileList.push({
          ...file,
          url: data.data
        });
        that.setData({
          fileList
        });
      },
    });
  },
  // 点击删除图片
  deleteImg() {
		Notify('点击了删除图片')
    app.request({
      url: app.globalData.fileApi + '/pic',
      method: 'DELETE',
      data: {
        fileName: this.data.fileList[0].url,
      }
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          fileList: [],
        })
        this.data.user.icon=null;
        wx.setStorageSync('user', this.data.user)
      } else {
        Notify('服务错误，稍后重试');
      }
    })
  },
  submit(e) {
    var userTemp = this.data.user;
    if (this.data.fileList.length>0) {
			console.log(this.data.fileList[0].url.length)
			if(this.data.fileList[0].url.length>51){
				userTemp.icon=wx.getStorageSync('user').icon
			}else{
				userTemp.icon = this.data.fileList[0].url
			}
			console.log(userTemp.icon)
    }
    userTemp.name=this.data.user.name
    var json = JSON.stringify(userTemp)
    app.request({
      url: app.globalData.apiUrl + '/user/update/' + json,
      method: 'PUT',
    }).then(res => {
      if (res.statusCode == 200) {
				wx.setStorageSync('user', userTemp)
				Notify({
					type: 'primary',
					message: '修改成功'
				})
        setTimeout((
				) => {
          wx.navigateBack({})
        }, 1000);
      } else {
        Notify('服务错误，稍后重试');
      }
    })
	},
	setName(e){
		console.log(e)
		this.setData({
				['user.name']:e.detail.value
		})
	}
})